package com.phrp.service.impl;

import com.phrp.exception.CustomizationException;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.phrp.entity.Notice;
import com.phrp.dao.NoticeDao;
import com.phrp.entity.vo.PageData;
import com.phrp.entity.vo.SearchNoticeDataVO;
import com.phrp.entity.vo.UpdateDataVO;
import com.phrp.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-04-06
 */
@Service
public class NoticeserviceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    /**
     * 新增公告
     * @param noticeData
     * @return
     */
    @Override
    public ResponseResult addNotice(Notice noticeData) {
        noticeData.setNUuid(UUID.randomUUID().toString().replace("-",""));
        noticeData.setCreateTime(LocalDateTime.now());
        noticeData.setUpdateTime(LocalDateTime.now());

        int i = noticeDao.insert(noticeData);

        if(i != 1){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 根据条件搜索内容
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult<PageData<LinkedList<Notice>>> searchBySearchData(SearchNoticeDataVO searchData) {
        searchData.setStartPage(searchData.getPageSize()*(searchData.getCurrentPage()-1));

        // 解析前后时间段
        if (searchData.getReleaseTime().size() > 0) {
            // 解析并转换第一个时间
            LocalDateTime startDateTime = LocalDateTime.parse(searchData.getReleaseTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setStartTime(startDateTime.toLocalDate().toString());

            // 解析并转换第二个时间
            LocalDateTime endDateTime = LocalDateTime.parse(searchData.getReleaseTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setEndTime(endDateTime.toLocalDate().toString());
        }

        // 进行数据库查询
        LinkedList<Notice> list = noticeDao.selectBySearchData(searchData);

        Integer i = noticeDao.getTotalNum(searchData);

        PageData<LinkedList<Notice>> pageData = new PageData<>();

        pageData.setData(list);
        pageData.setTotalNumber(i);

        return new ResponseResult<>(ReturnInfoEnum.SUCCESS,pageData);

    }

    /**
     * 修改公告状态
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult updateStatus(UpdateDataVO data) {
        LocalDateTime releaseTime = null;
        String type = data.getType();
        if(type.equals("1")){
            releaseTime = LocalDateTime.now();
        }
        Integer i = noticeDao.updateByIds(data.getIds(),data.getType(),releaseTime);
        if(i != data.getIds().size()){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 删除公告
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult deleteNotice(LinkedList<String> ids) {
        int i = noticeDao.deleteBatchIds(ids);
        if(i != ids.size()){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 编辑
     * @param notice
     * @return
     */
    @Override
    public ResponseResult edit(Notice notice) {
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("n_uuid",notice.getNUuid());

        int i = noticeDao.update(notice, updateWrapper);
        if(i != 1){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }
}
